home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Format / ascii2fax / image2fax.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  4.8 KB  |  323 lines

  1. /* image2fax.c: conversion routines from PP image to x400 fax */
  2. /* code "borrowed" from QUIPU */
  3.  
  4. # ifndef lint
  5. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Format/ascii2fax/RCS/image2fax.c,v 6.0 1991/12/18 20:15:19 jpo Rel $";
  6. # endif
  7.  
  8. /*
  9.  * $Header: /xtel/pp/pp-beta/Format/ascii2fax/RCS/image2fax.c,v 6.0 1991/12/18 20:15:19 jpo Rel $
  10.  *
  11.  * $Log: image2fax.c,v $
  12.  * Revision 6.0  1991/12/18  20:15:19  jpo
  13.  * Release 6.0
  14.  *
  15.  */
  16.  
  17. #include    <stdio.h>
  18. #include    "util.h"
  19. #include    "IOB-types.h"
  20. #include    "g3.h"
  21. #include     <varargs.h>
  22. #include    "fonts.h"
  23.  
  24. # define BITSPERBYTE    8
  25.  
  26. void    advise ();
  27. void    adios ();
  28.  
  29. #define ps_advise(ps, f) \
  30.     advise (NULLCP, "%s: %s", (f), ps_error ((ps) -> ps_errno))
  31.  
  32. static char    *store = NULLCP;
  33. static int    storeix = 0, store_allocd = 0;
  34. static void    putwhitespan(), putblackspan(),
  35.         putcode(), puteol(), putinit(),
  36.         myputchar(), putbit();
  37. static int shdata;
  38. static int shbit = 0x80;
  39.  
  40. encodeImage(page, wd, ht, fax)
  41. BitMap    page;
  42. unsigned int    wd, ht;
  43. struct type_IOB_G3FacsimileBodyPart    *fax;
  44. {
  45.     int        row;
  46.  
  47.     putinit();
  48.  
  49.     for(row = 0; row < ht; row++) {
  50.  
  51.         tofax(page,row,wd);
  52.     
  53.     }
  54.        
  55.     for( row = 0; row < 5; row++ )
  56.         puteol( );
  57.     
  58.     flushbits(fax);
  59.  
  60. }
  61.  
  62. /*   */
  63.  
  64. /* encode a row of the image */
  65.  
  66. tofax(image,row,wd)
  67. BitMap    image;
  68. int    row;
  69. unsigned int    wd;
  70. {
  71.     int c = 0;
  72.     int    n = 0;
  73.  
  74.     while(n < wd) { 
  75.     c = 0;
  76.     while(!BL_ISSET(n, image[row]) && (n < wd)) {
  77.         c++;
  78.         n++;
  79.     }
  80.     putwhitespan(c);
  81.     c = 0;
  82.     if(n == wd)
  83.         break;
  84.     while(BL_ISSET(n, image[row]) && (n < wd)) {
  85.         c++;
  86.         n++;
  87.     }
  88.     putblackspan(c);
  89.     }
  90.     puteol();
  91. }
  92.  
  93. /*   */
  94. /* wrap up encoded page in ASN1 */
  95.  
  96. flushbits(fax)
  97. struct type_IOB_G3FacsimileBodyPart    *fax;
  98. {
  99.     struct type_IOB_G3FacsimileData    *new, *ix;
  100.     
  101.     if (shbit != 0x01 ){
  102.         myputchar(shdata);
  103.         shdata = 0;
  104.         shbit = 0x01;
  105.     }
  106.     
  107.     new = (struct type_IOB_G3FacsimileData *) 
  108.         calloc (1, sizeof(struct type_IOB_G3FacsimileData));
  109.     new -> element_IOB_0 = strb2bitstr (store,
  110.                        (storeix * BITSPERBYTE),
  111.                        PE_CLASS_UNIV,
  112.                        PE_PRIM_BITS);
  113.     if (fax -> data == NULL)
  114.         fax -> data = new;
  115.     else {
  116.         for (ix = fax -> data; ix -> next != NULL; ix = ix -> next);
  117.         ix -> next = new;
  118.     }
  119.     fax -> parameters -> number__of__pages++;
  120.     storeix = 0;
  121. }
  122.  
  123. /*   */
  124.  
  125.  
  126. /*   */
  127. /* encode and splurt out the fax */
  128.  
  129. outputFax (fax,fp)
  130. struct type_IOB_G3FacsimileBodyPart    *fax;
  131. FILE                    *fp;
  132. {
  133.     PE    pe = NULLPE;
  134.     PS    psout;
  135.  
  136.     encode_IOB_G3FacsimileBodyPart (&pe, 1, 0, NULLCP, fax);
  137.  
  138.     /* IMPLICIT TAG */
  139.     pe->pe_class = PE_CLASS_CONT;
  140.     pe->pe_id = 3;
  141.  
  142.     /* output */
  143.     if ((psout = ps_alloc(std_open)) == NULLPS) {
  144.         ps_advise (psout, "ps_alloc");
  145.         exit(1);
  146.     }
  147.  
  148.     if (std_setup (psout, fp) == NOTOK) {
  149.         advise (NULLCP, "std_setup loses");
  150.         exit (1);
  151.     }
  152.  
  153.     if (pe2ps(psout, pe) == NOTOK) {
  154.         ps_advise(psout, "pe2ps loses");
  155.         exit(1);
  156.     }
  157.  
  158.     ps_free(psout);
  159.  
  160.     if (pe != NULLPE) pe_free (pe);
  161. }
  162.  
  163. /*   */
  164. /* basic output routines for fax bits */
  165.  
  166. static void putwhitespan(c)
  167. int c;
  168. {
  169.     int tpos;
  170.     tableentry *te;
  171.  
  172.     if(c>=64) {
  173.     tpos = (c/64)-1;
  174.     te = mwtable+tpos;
  175.     c -= te->count;
  176.     putcode(te);
  177.     }
  178.     tpos = c;
  179.     te = twtable+tpos;
  180.     putcode(te);
  181. }
  182.  
  183. static void putblackspan(c)
  184. int c;
  185. {
  186.     int tpos;
  187.     tableentry *te;
  188.  
  189.     if(c>=64) {
  190.     tpos = (c/64)-1;
  191.     te = mbtable+tpos;
  192.     c -= te->count;
  193.     putcode(te);
  194.     }
  195.     tpos = c;
  196.     te = tbtable+tpos;
  197.     putcode(te);
  198. }
  199.  
  200. static void putcode(te)
  201. tableentry *te;
  202. {
  203.     unsigned int mask;
  204.     int code;
  205.  
  206.     mask = 1<<(te->length-1);
  207.     code = te->code;
  208.     while(mask) {
  209.      if(code&mask)
  210.         putbit(1);
  211.     else
  212.         putbit(0);
  213.     mask >>= 1;
  214.     }
  215.  
  216. }
  217.  
  218. static void puteol()
  219. {
  220.     int i;
  221.  
  222.     for(i=0; i<11; i++)
  223.     putbit(0);
  224.     putbit(1);
  225. }
  226.  
  227. static void putinit()
  228. {
  229.     shdata = 0;
  230.     shbit = 0x01;
  231.     storeix = 0;
  232.     puteol();
  233. }
  234.  
  235. #define    INC    512
  236.  
  237. extern unsigned char flip_bits();
  238.  
  239. static void myputchar (ch)
  240. char    ch;
  241. {
  242.     if (storeix == store_allocd) {
  243.         /* resize */
  244.         store_allocd += INC;
  245.         if (store != NULLCP) 
  246.             store = realloc (store, 
  247.                      (unsigned) (store_allocd * sizeof (char)));
  248.         else
  249.             store = calloc ((unsigned) store_allocd,
  250.                     sizeof (char *));
  251.     }
  252.     store[storeix++] = flip_bits((unsigned char) ch);
  253. }
  254.  
  255. static void putbit(d)
  256. int d;
  257. {
  258.     if(d) 
  259.     shdata = shdata|shbit;
  260.     shbit = shbit<<1;
  261.  
  262.     if((shbit&0xff) == 0) {
  263.     myputchar(shdata);
  264.     shdata = 0;
  265.     shbit =  0x01;
  266.     }
  267. }
  268.  
  269. /*     ERRORS */
  270.  
  271. #ifndef lint
  272. void    adios (va_alist)
  273. va_dcl
  274. {
  275.     va_list ap;
  276.  
  277.     va_start (ap);
  278.  
  279.     _ll_log (pp_log_norm, LLOG_FATAL, ap);
  280.  
  281.     va_end (ap);
  282.  
  283.     _exit (1);
  284. }
  285. #else
  286. /* VARARGS2 */
  287.  
  288. void    adios (what, fmt)
  289. char   *what,
  290.        *fmt;
  291. {
  292.     adios (what, fmt);
  293. }
  294. #endif
  295.  
  296.  
  297. #ifndef lint
  298. void    advise (va_alist)
  299. va_dcl
  300. {
  301.     int     code;
  302.     va_list ap;
  303.  
  304.     va_start (ap);
  305.  
  306.     code = va_arg (ap, int);
  307.  
  308.     _ll_log (pp_log_norm, code, ap);
  309.  
  310.     va_end (ap);
  311. }
  312. #else
  313. /* VARARGS3 */
  314.  
  315. void    advise (code, what, fmt)
  316. char   *what,
  317.        *fmt;
  318. int     code;
  319. {
  320.     advise (code, what, fmt);
  321. }
  322. #endif
  323.